﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twitter;
using System.ComponentModel.Design;
using FriendsNearby.Services;
using FriendsNearby.Services.Facebook;
using FriendsNearby.Services.Entities;
using InSTEDD.Labs.Geo;

namespace FriendsNearbyBot
{
    internal class PeopleNearbyProcessor : IMessageProcessor
    {
        IUsersService userService;
        IPeopleNearbyService peopleService;
        IMessageBuilder messageBuilder;

        public PeopleNearbyProcessor(IUsersService userService, IPeopleNearbyService peopleService)
            : this(userService, peopleService, new ListMessageBuilder())
        {
        }

        public PeopleNearbyProcessor(IUsersService userService, IPeopleNearbyService peopleService, IMessageBuilder messageBuilder)
        {
            this.userService = userService;
            this.peopleService = peopleService;
            this.messageBuilder = messageBuilder;
        }

        public bool CanProcess(Message message) 
        {
            return message.Text == "list near";
        }

        public RunNextProcessor Process(IBotClient client, Message message)
        {
            if (CanProcess(message))
			{
				UserRegistry user = userService.QueryUserRegistry(x => x.TwitterScreenName == message.Sender.ScreenName);


                messageBuilder.Clear();

				if (user == null)
                {
                    // Warn the user via SMS that the mapping has not been established.                    
                    messageBuilder.AppendFragment(String.Format(Properties.Resources.TwitterNotMapped, message.Sender.ScreenName));
                    client.SendMessages(message.Sender.ScreenName, messageBuilder.GetMessages(),message,this.GetType().Name);
                }
                else
                {
					IEnumerable<PersonNearby> peopleNearby = peopleService.GetContacts(user.UserID, QueryContactsKind.Nearby);

                    var peopleNearbyEmpty = true;
                    foreach (PersonNearby pn in peopleNearby)
                    {
                        peopleNearbyEmpty = false;
                        messageBuilder.AppendFragment(String.Format(Properties.Resources.PeopleNearbyResponse,
                                                            pn.DisplayName,
                                                            DistanceHelper.GetDistanceString(pn.Distance)));
                    }
                    if (peopleNearbyEmpty)
                    {
                        messageBuilder.AppendFragment(Properties.Resources.PeopleNearbyEmptyResponse);
                    }

                    client.SendMessages(message.Sender.ScreenName, messageBuilder.GetMessages(), message, this.GetType().Name);
                }

                BotProcessor.MessageProcessed(client, message);

                return RunNextProcessor.No;

            }
            else
                return RunNextProcessor.Yes;

            

        }
    }
}
